using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace sforceExplorer
{
	public class frmAddAccount : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtAccountName;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtAccountNumber;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cboIndustry;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.Button btnCancel;
		private System.ComponentModel.Container components = null;

		public frmAddAccount()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.txtAccountName = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txtAccountNumber = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.cboIndustry = new System.Windows.Forms.ComboBox();
			this.btnAdd = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtAccountName
			// 
			this.txtAccountName.Location = new System.Drawing.Point(136, 16);
			this.txtAccountName.Name = "txtAccountName";
			this.txtAccountName.Size = new System.Drawing.Size(144, 20);
			this.txtAccountName.TabIndex = 0;
			this.txtAccountName.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(104, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Account name:";
			// 
			// txtAccountNumber
			// 
			this.txtAccountNumber.Location = new System.Drawing.Point(136, 48);
			this.txtAccountNumber.Name = "txtAccountNumber";
			this.txtAccountNumber.Size = new System.Drawing.Size(144, 20);
			this.txtAccountNumber.TabIndex = 0;
			this.txtAccountNumber.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(104, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Account number:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(104, 16);
			this.label3.TabIndex = 1;
			this.label3.Text = "Industry:";
			// 
			// cboIndustry
			// 
			this.cboIndustry.Location = new System.Drawing.Point(136, 80);
			this.cboIndustry.Name = "cboIndustry";
			this.cboIndustry.Size = new System.Drawing.Size(144, 21);
			this.cboIndustry.TabIndex = 2;
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(88, 120);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.Size = new System.Drawing.Size(88, 24);
			this.btnAdd.TabIndex = 3;
			this.btnAdd.Text = "Add";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(192, 120);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// frmAddAccount
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(320, 166);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.cboIndustry);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtAccountName);
			this.Controls.Add(this.txtAccountNumber);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.btnCancel);
			this.Name = "frmAddAccount";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Partner Sample - Add Account";
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		public void loadIndustryCombo() 
		{
			sforce.PicklistEntry[] pl = Utility.GetPickList("Account", "Industry");
			for (int j=0;j<pl.Length;j++) 
				this.cboIndustry.Items.Add(pl[j].value);
			this.cboIndustry.SelectedIndex = 0;
			return;
		}
		
		private System.Xml.XmlElement MakeFieldElement(string fieldName, string fieldValue) 
		{
			System.Xml.XmlDocument frag = new System.Xml.XmlDocument();
			System.Xml.XmlElement el = frag.CreateElement(fieldName);
			el.InnerText = fieldValue;
			return el;
		}
		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			if (this.txtAccountName.Text.Length > 0) 
			{
				sforce.sObject newAccount = new sforce.sObject();
				newAccount.type = "Account";
				System.Xml.XmlElement[] records = new System.Xml.XmlElement[3];
				records[0] = MakeFieldElement("Name", this.txtAccountName.Text);
				records[1] = MakeFieldElement("AccountNumber", this.txtAccountNumber.Text);
				records[2] = MakeFieldElement("Industry", this.cboIndustry.Text);
				newAccount.Any = records;
				try 
				{
					sforce.SaveResult[] saveResult = Utility.binding.create(new sforce.sObject[] {newAccount});
					this.Close();
				}
				catch (Exception ex) 
				{
					System.Windows.Forms.MessageBox.Show("Create failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				}
			}
		}
	}
}
